import Vue from "vue";
import axios from "axios";
import router from "./router";

const http = axios.create({
  baseURL: process.env.VUE_APP_API_URL || "/admin/api"
  // baseURL: "http://localhost:3000/admin/api"
});

// 请求拦截器 Authorization
http.interceptors.request.use(
  config => {
    if (localStorage.token) {
      config.headers.Authorization = "Bearer " + localStorage.token;
    }
    return config;
  },
  error => {
    return Promise.reject(error);
  }
);

// 通用响应拦截
http.interceptors.response.use(
  res => {
    return res;
  },
  err => {
    if (err.response.data.message) {
      Vue.prototype.$message({
        type: "error",
        message: err.response.data.message
      });
    }
    if (err.response.status === 401) {
      router.push("/login");
    }
    // console.log(err.response.data.message);
    return Promise.reject(err);
  }
);

export default http;
